#include "GameManager.h"

GameManager::GameManager()
{
    if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
    }

    screen = SDL_SetVideoMode(1024, 768, 32,SDL_HWSURFACE|SDL_DOUBLEBUF);

    TTF_Init();
    Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024);

    timer = SDL_GetTicks();
    dt = timer;

    manex = (FPSmanager*) malloc(sizeof(FPSmanager));
    SDL_initFramerate(manex);
    SDL_setFramerate(manex, 30);


    currentState = new SplashState();
    currentState->Load();

}

void GameManager::run()
{
    Uint32 timer, dt, atimer;

    bool done = false;
    timer = 0;
    while (!done)
    {

        switch(currentState->Update(dt))
        {
            case 0:
                currentState->Render(screen);
                break;
            case 1:
                currentState->Unload();
                currentState = new SplashState();
                currentState->Load();
                break;
            case 2:
                currentState->Unload();
                currentState = new MapState();
                currentState->Load();
                break;
            case -1:
                currentState->Unload();
                done = true;
                break;
        }

        atimer = timer;
        timer = SDL_GetTicks();
        dt = timer - atimer;

        //RENDER



        // finally, update the screen :)
        SDL_Flip(screen);
        SDL_framerateDelay(manex);
    } // end main loop

    // free loaded bitmap
    //SDL_FreeSurface(bmp);

}

